package com.epro.dx.adapter;

import java.util.List;

import android.content.ActivityNotFoundException;
import com.epro.dx.R;
import com.epro.dx.activity.LPLocalBrowserActivity;
import com.epro.dx.activity.SpecialActivity;
import com.epro.dx.activity.search.SearchActivity;
import com.epro.dx.indicator.IconPagerAdapter;
import com.epro.dx.util.ImageLoaderUtils;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.facade.model.ModuleChildrenModel;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.weipu.common.util.Logger;

public class HomeAdvertisementAdapter extends BaseAdapter {
	private final static String TAG = "HomeBannerAdapter";
	private Context mContext;

	private List<ModuleChildrenModel> advertModels;

	private DisplayImageOptions options;

	private onLinkBannerClickListenner onclickListenner;

	public HomeAdvertisementAdapter(Context mContext,
			List<ModuleChildrenModel> advertModels) {
		this.mContext = mContext;
		this.advertModels = advertModels;
		options = ImageLoaderUtils.getImageLoaderOptions(
				R.drawable.default488_488, R.drawable.default488_488);
	}

	public void setOnLinkBannerClickListenner(
			onLinkBannerClickListenner onclickListenner) {
		this.onclickListenner = onclickListenner;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (null != advertModels) {
			return advertModels.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (null != advertModels) {
			advertModels.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.image_home_banner,
					null);
			holder.advImg = (ImageView) convertView.findViewById(R.id.img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ModuleChildrenModel mModel = advertModels.get(position);
		if(null==mModel){
			return null;
		}
		boolean  isDefault=mModel.isDefault();
		if(isDefault){
			holder.advImg.setImageResource(mModel.getResId());
		}else{
			String filePath = mModel.getPhoneImg();
			if (!TextUtils.isEmpty(filePath) && null != holder.advImg) {
				holder.advImg.setTag(filePath);
				// imageWorker.loadImage(filePath, imageView);
				ImageLoader.getInstance().displayImage(filePath, holder.advImg,
						options);
			} else {
				holder.advImg.setImageResource(R.drawable.default488_488);

			}
		}
		
//		holder.advImg.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if (null != mModel) {
//					
//				}
//			}
//		});

		return convertView;
	}

	class ViewHolder {
		ImageView advImg;// 描述信息
	}

	public interface onLinkBannerClickListenner {
		void onClickBanner(String bannerid);
	}
}
